<template>
	<view>
		<van-sticky>
			<view class="topTime">
			<van-count-down :time="time_limit" @change="outTime" @finish="timeEnd">
				<template #default="timeData">
					<span class="block">{{ timeData.hours }}</span>
					<span class="colon">:</span>
					<span class="block">{{ timeData.minutes }}</span>
					<span class="colon">:</span>
					<span class="block">{{ timeData.seconds }}</span>
				</template>
			</van-count-down>
		</view>
		</van-sticky>
		
		<view class="topicBody">
			<view>
				<h3 class="title">单选题</h3>
				<view class="topic" v-for="item,index in singles" :key="index">
					<text><b>{{index+1}}.</b>{{item.single_name}}</text>
					<van-radio-group v-model="singles_topic[index].single_key">
						<van-radio name="A" class="option">{{item.A}}</van-radio>
						<van-radio name="B" class="option">{{item.B}}</van-radio>
						<van-radio name="C" class="option">{{item.C}}</van-radio>
						<van-radio name="D" class="option">{{item.D}}</van-radio>
					</van-radio-group>
				</view>
			</view>
			<view>
				<h3 class="title">多选题</h3>
				<view class="topic" v-for="item,index in selections" :key="index">
					<text><b>{{index+1}}.</b>{{item.selection_name}}</text>
					<van-checkbox-group v-model="selections_topic[index].selection_key">
						<van-checkbox name="A" class="option">{{item.A}}</van-checkbox>
						<van-checkbox name="B" class="option">{{item.B}}</van-checkbox>
						<van-checkbox name="C" class="option">{{item.C}}</van-checkbox>
						<van-checkbox name="D" class="option">{{item.D}}</van-checkbox>
					</van-checkbox-group>
				</view>
			</view>
			<view>
				<h3 class="title">判断题</h3>
				<view class="topic" v-for="item,index in judges" :key="index">
					<text><b>{{index+1}}.</b>{{item.judge_name}}</text>
					<van-radio-group v-model="judges_topic[index].judge_key">
						<van-radio :name="0" class="option">错误</van-radio>
						<van-radio :name="1" class="option">对</van-radio>
					</van-radio-group>
				</view>
			</view>
		</view>
		<van-button type="info" block @click="sumbit">提交</van-button>
	</view>
</template>

<script>
	import {getStudentAppExamQuestions,studentAppSubmitExam} from "@/api/user"
	import {Formatter} from "@/utils/diy_funtion"
	import {Toast} from 'vant';
	export default {
		data() {
			return {
				time_limit: 2000000000,
				time: null,
				test_id: null,
				judges: {},
				selections: {},
				singles: {},
				singles_topic: [],
				selections_topic: [],
				judges_topic: [],
				test_UseTime: null
			}
		},
		onLoad: function(data) {
			this.test_id = data.test_id
			getStudentAppExamQuestions({
				key: data.test_id
			}).then(res => {
				const {
					time_limit,
					judges,
					selections,
					singles
				} = res.data.data
				this.time_limit = 60 * parseInt(time_limit) * 1000
				console.log(this.time_limit);
				this.time = parseInt(time_limit)
				this.judges = judges
				this.selections = selections
				this.singles = singles
				this.singles_topic = []
				this.selections_topic = []
				this.judges_topic = []
				this.singles.forEach(item => {
					this.singles_topic.push({
						single_id: item.key,
						single_key: null
					})
				})
				this.selections.forEach(item => {
					this.selections_topic.push({
						selection_key: [],
						selection_id: item.key
					})
				})
				this.judges.forEach(item => {
					this.judges_topic.push({
						judge_id: item.key,
						judge_key: null
					})
				})

			})
		},
		methods: {
			Formatter: Formatter,
			timeEnd:function(){
				studentAppSubmitExam({
					test_id: this.test_id,
					student_id: this.$store.getters.student_id,
					consume_time: this.test_UseTime,
					cheating: 0,
					singles: this.singles_topic,
					selections: this.selections_topic,
					judges: this.judges_topic
				}).then(res => {
					Toast({
						message: "时间结束强制交卷",
						position: 'top'
					})
					uni.switchTab({
						url: '../home/index'
					})
				})
			},
			outTime: function(e) {
				let useTime = this.time - (e.hours * 60 + e.minutes)
				this.test_UseTime = useTime
			},
			sumbit: function() {
				for (let i in this.selections_topic) {
					if (this.selections_topic[i].selection_key.length < 1) {
						Toast({
							message: "你还有题目没做完",
							position: 'top'
						})
						return
					}
				}
				for (let o in this.singles_topic) {
					if (this.singles_topic[o].single_key == null) {
						Toast({
							message: "你还有题目没做完",
							position: 'top'
						})
						return
					}
				}
				for (let j in this.judges_topic) {
					if (this.judges_topic[j].judge_key == null) {
						Toast({
							message: "你还有题目没做完",
							position: 'top'
						})
						return
					}
				}
				// this.judges_topic.forEach(item => {
				// 	if (item.judge_key == "0") {
				// 		item.judge_key = 0
				// 	} else {
				// 		item.judge_key = 1
				// 	}
				// })
				studentAppSubmitExam({
					test_id: this.test_id,
					student_id: this.$store.getters.student_id,
					consume_time: this.test_UseTime,
					cheating: 0,
					singles: this.singles_topic,
					selections: this.selections_topic,
					judges: this.judges_topic
				}).then(res => {
					Toast({
						message: res.data.msg,
						position: 'top'
					})
					uni.switchTab({
						url: '../home/index'
					})
				})
			}
		}
	}
</script>

<style>
	.topTime {
		width: 100%;
		height: 88rpx;
		background-color: white;
		color: #ffffff;
		justify-content: center;
		display: flex;
		align-items: center;
	}

	.topic {
		margin: 20rpx 0;
	}

	.topic b {
		margin-right: 10rpx;
		font-size: 40rpx;
	}

	.title {
		margin: 10rpx;
	}

	.topicBody {
		padding: 20rpx;
	}

	.option {
		margin: 20rpx;
	}

	.colon {
		display: inline-block;
		margin: 0 4px;
		color: #000000;
	}

	.block {
		display: inline-block;
		width: 22px;
		color: #fff;
		font-size: 12px;
		text-align: center;
		background-color: #ee0a24;
		border-radius: 10rpx;
	}
</style>
